% This function reconstruct the signal from the recovered support and the
% samples
function [RecSignal RecTimeAxis] = RecoverSignal(Signal,MWC,Support,Samples,TimeAxis,A)

[m,NumSamples] = size(Samples);
[m,M] = size(A);

Zn = zeros(M,NumSamples);
As = A(:,Support);
Zn(Support,:) = pinv(As)*Samples;


Ts = TimeAxis(2)-TimeAxis(1);
T = Ts/M;

L=MWC.L;

if (mod(L,2)==0)
    L=L+1;
end   % L is the number of specturm slices, namely the length of z[n]
L0 = (L-1)/2;

RecTimeAxis = linspace(   (min(TimeAxis)-L0*T),  (max(TimeAxis)+L0*T),  M*NumSamples );
% RecTimeAxis = TimeAxis; %ETGAR - We have to sample fisrt the original in nyq rate

RecSignal=zeros(size(RecTimeAxis));

for Zind = 1:L0
    Zn_int = interpft(Zn(Zind,:),M*NumSamples);
    % trying other interpulation: - SHAHAR & ETGAR
%     t=linspace(RecTimeAxis(1),RecTimeAxis(end),length(Zn(Zind,:)));
%     Zn_int = interp1(t,Zn(Zind,:),RecTimeAxis,'spline');    
    
    slice = Zind - L0 -1;
%     Mod = exp(1j*2*pi*slice/M*RecTimeAxis/T); %SHAHAR - Tried to the
%                                                           modulation with cosine function
%      Mod = 2*cos(2*pi*slice/M*RecTimeAxis/T);
     Mod = exp(1j*2*pi*slice*(1/Ts)*RecTimeAxis); %ETGAR - make it it consistence to the Demo
    RecSignal = RecSignal + Zn_int.*Mod;
%     updateProgBar(Zind,handles,(L0+1)*2);    
end

Z0_int = interpft(Zn(L0+1,:),M*NumSamples);
% updateProgBar(L0+1,handles,(L0+1)*2);
RecSignal = RecSignal + Z0_int;
RecSignal=real(RecSignal);
% ClearBar(handles);
% set(handles.uneditProgPrecent,'String','');
% set(handles.uneditStatus,'String','Status: Ready (waiting for user input)');
